package com.fangjinwang.zhushunqing.Widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fangjinwang.zhushunqing.App.FJWApplication;
import com.fangjinwang.zhushunqing.R;

/**
 * Created by admin on 2016/11/3.
 */
public class DrawableTextTop extends LinearLayout {

//    private ImageView mButtonImage = null;
//    private TextView mButtonText = null;
//    private int textSize = 18;
//
//    public DrawableTextTop(Context context, AttributeSet attrs) {
//        super(context,attrs);
//        init(context);
//    }
//
//    /**
//     *
//     * @param context
//     * @param intArray intArray[0] : ImageResourceId;
//     * intArray[1] : textResourceId; intArray[2] : textSize. Other intArray is useless
//     */
//    public DrawableTextTop(Context context, int... intArray) {
//        super(context);
//
//        // Init instance
//
//    }
//    public void init(Context context, int... intArray){
//        mButtonImage = new ImageView(context);
//        mButtonText = new TextView(context);
//
//        int len = intArray.length;
//        if (len >= 1) {
//            // Set Image Resource
//            setImageResource(intArray[0]);
//        }
//        if (len >= 2) {
//            // Set Text
//            setText(intArray[1]);
//        }
//        if (len >= 3) {
//            // Change text size
//            textSize = intArray[2];
//        }
//
//        /** Set Child View(ImageView/TextView) properties */
//        // Set Text Size : default 18
//        setTextSize(textSize);
//        // Set ImageView ScaleType : default CENTER_INSIDE(located center, without resize)
//        setImgScaleType(ImageView.ScaleType.CENTER_INSIDE);
//        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.5f);
//        // Set ImageView LayoutParams : default half
//        setImgLayoutParams(layoutParams);
//        // Set TextView LayoutParams : default half
//        setTextLayoutParams(layoutParams);
//        // Set Text Gravity : default center
//        setTextGravity(Gravity.CENTER);
//        // Set Text Color : default white
//        setTextColor(0xFFFFFFFF);
//
//        /** Set Father View(LinearLayout) properties */
//        setClickable(true);
//        setFocusable(true);
//        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
//                LayoutParams.MATCH_PARENT, 1);
////      params.gravity = Gravity.CENTER;
//        // Set Father View Orientation : default fulfill
//        setFatherViewLayoutParams(params);
//        // Set Father View Orientation : default my_img_btn_default
//        setFatherViewBgResource(R.mipmap.icon_huodong);
//        // Set Father View Orientation : default VERTICAL
//        setFatherViewOrientation(LinearLayout.VERTICAL);
//
//        addView(mButtonImage);
//        addView(mButtonText);
//    }
//
//
//
//    /*
//     * setImageResource
//     */
//    public void setImageResource(int resId) {
//        mButtonImage.setImageResource(resId);
//    }
//
//    /*
//     * setText
//     */
//    public void setText(int resId) {
//        mButtonText.setText(resId);
//    }
//
//    public void setText(CharSequence buttonText) {
//        mButtonText.setText(buttonText);
//    }
//
//    /*
//     * setTextColor
//     */
//    public void setTextColor(int color) {
//        mButtonText.setTextColor(color);
//    }
//
//    /*
//     * setTextSize
//     */
//    public void setTextSize(int textSize) {
//        mButtonText.setTextSize(textSize);
//    }
//
//    /**
//     * @param layoutParams the layoutParams to set
//     */
//    public void setImgLayoutParams(LayoutParams layoutParams) {
//        mButtonImage.setLayoutParams(layoutParams);
//    }
//
//    /**
//     * Controls how the image should be resized or moved to match the size of this ImageView
//     *
//     * @param layoutParams the layoutParams to set
//     */
//    public void setImgScaleType(ImageView.ScaleType scaleType) {
//        mButtonImage.setScaleType(scaleType);
//    }
//
//    /**
//     * @param layoutParams the layoutParams to set
//     */
//    public void setTextLayoutParams(LayoutParams layoutParams) {
//        mButtonText.setLayoutParams(layoutParams);
//    }
//
//    /**
//     * @param gravity the gravity to set
//     */
//    public void setTextGravity(int gravity) {
//        mButtonText.setGravity(gravity);
//    }
//
//    /**
//     * Set Father View LayoutParams. Notice that this method should not be used generally.
//     *
//     * @param params
//     */
//    public void setFatherViewLayoutParams(LayoutParams params) {
//        super.setLayoutParams(params);
//    }
//
//    public void setFatherViewBgResource(int resId) {
//        super.setBackgroundResource(resId);
//    }
//
//    /**
//     * Set orientation of this Linearlayout. Notice that since the default orientation is vertical,
//     * when you use this method to modify the orientation to horizontal , make sure that you
//     * also use {@link #setImgLayoutParams(LayoutParams layoutParams)} and
//     * {@link #setTextLayoutParams(LayoutParams layoutParams)} together, otherwise,
//     * the button can not be displayed normally
//     *
//     * @param orientation
//     */
//    public void setFatherViewOrientation(int orientation) {
//        super.setOrientation(orientation);
//    }


    private ImageView imageView;
    private TextView textView;


    public DrawableTextTop(Context context) {
        super(context);
        init(context);
    }

    public DrawableTextTop(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextTop);
        text_color = a.getColor(R.styleable.DrawableTextTop_text_color, ContextCompat.getColor(context, R.color.black));//
        text = a.getString(R.styleable.DrawableTextTop_text);
        ResId = a.getResourceId(R.styleable.DrawableTextTop_src, R.mipmap.icon_anquan);
        init(context);

    }

    private int text_color = R.color.black;
    private String text;
    private int ResId = R.mipmap.icon_anquan;

    private void init(Context context) {


        setOrientation(VERTICAL);
        imageView = new ImageView(context);
        textView = new TextView(context);

//        View view = LayoutInflater.from(context).inflate(R.layout.layout_drawable_top, null, false);
//        imageView = (ImageView) view.findViewById(R.id.layout_drawable_top_image);
//        textView = (TextView)view.findViewById(R.id.layout_drawable_top_text);

//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 50);
//        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(50, 50);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        imageView.setImageResource(ResId);
        textView.setText(text);
        lp.gravity = Gravity.CENTER_HORIZONTAL;

        imageView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        imageView.setLayoutParams(lp);
        textView.setLayoutParams(lp);
//        imageView.(Gravity.CENTER);

        textView.setTextColor(FJWApplication.getAppResources().getColor(R.color.text_black));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);//FJWApplication.getAppResources().getDimension(R.dimen.font_normal_big);

        addView(imageView);
        addView(textView);
//        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) imageView.getLayoutParams();

    }

    public void setText(CharSequence text) {
        textView.setText(text);
    }

    public void setImage(int resources) {
        imageView.setImageResource(resources);
    }
}
